package cn.mily.pet.service.impl;

import cn.mily.Start;
import cn.mily.basics.query.BaseQuery;
import cn.mily.basics.service.impl.BasicServiceImpl;
import cn.mily.basics.utils.PageBean;
import cn.mily.org.domain.Shop;
import cn.mily.org.mapper.ShopMapper;
import cn.mily.pet.domain.Pet;
import cn.mily.pet.domain.PetDetail;
import cn.mily.pet.domain.PetType;
import cn.mily.pet.domain.SourceType;
import cn.mily.pet.mapper.PetDetailMapper;
import cn.mily.pet.mapper.PetMapper;
import cn.mily.pet.mapper.PetTypeMapper;
import cn.mily.pet.mapper.SearchMapper;
import cn.mily.pet.query.FindMastQuery;
import cn.mily.pet.service.IPetService;
import cn.mily.user.domain.LoginInfo;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.xml.soap.Detail;
import java.util.List;

@Service
public class PetServiceImpl extends BasicServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetMapper mapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private PetDetailMapper petDetailMapper;

    @Autowired
    private SearchMapper searchMapper;

    @Autowired
    private PetTypeMapper petTypeMapper;

    @Override
    @Transactional
    public void update(Pet pet) {
        /*修改宠物详情*/
        PetDetail detail = pet.getDetail();
        detail.setPet_id(pet.getId());
        petDetailMapper.update(pet.getDetail());
        /*x修改宠物*/
        super.update(pet);
    }

    /**
     * 根据id删除
     * @param id
     */
    @Override
    @Transactional
    public void delete(Long id) {
        Pet pet = mapper.findOne(id);
        /*判断是否寻主表中来的*/
        if(pet.getUser()!=null){
            /*删除寻主表中的*/
            searchMapper.deleteByUserId(pet.getUser().getId());
        }
        /*删除当前pet 中*/
        super.delete(id);
    }

    /**
     * 处理宠物信息
     * @param pet
     */
    @Transactional
    public void add(Pet pet, HttpServletRequest request) {
        /*获取请求中的token*/
        String token = request.getHeader("token");
        /*根据token在redis中获取登录信息*/
        RedisTemplate redisTemplate = Start.getRedisTemplate();
        LoginInfo loginInfo = (LoginInfo) redisTemplate.opsForValue().get(token);
        /*查找shop*/
        Shop shop = shopMapper.findByInfoId(loginInfo.getId());
        /*添加shop*/
        pet.setShop(shop);
        /*添加宠物信息*/
        /*SourceType sourceType = new SourceType();
        sourceType.setId(1L);
        pet.setSourceType(sourceType);*/
        super.add(pet);
        /*添加宠物详情*/
        PetDetail detail = pet.getDetail();
        detail.setPet_id(pet.getId());
        petDetailMapper.add(detail);
        /*修改寻主状态  已处理*/
        searchMapper.updateState(1);
    }

    /**
     * 宠物管理 + 列表展示
     * @param query
     * @param request
     * @return
     */
    @Override
    @Transactional(readOnly = true)
    public PageBean<Pet> findAll(FindMastQuery query, HttpServletRequest request) {
        /*获取请求中的token*/
        String token = request.getHeader("token");
        /*根据token在redis中获取登录信息*/
        RedisTemplate redisTemplate = Start.getRedisTemplate();
        LoginInfo loginInfo = (LoginInfo) redisTemplate.opsForValue().get(token);
        /*查找shop*/
        Shop shop = shopMapper.findByInfoId(loginInfo.getId());
        /*设置查询 shopid*/
        if(shop!=null){
            query.setShopid(shop.getId());
        }
        Integer count = mapper.getCount(query);/*查询结果数量*/
        if(count==null||count<=0){/*数量为0就直接退出*/
            return null;
        }
        PageBean<Pet> pageBean=new PageBean<>();
        List<Pet> pets = mapper.findAll(query);
        pageBean.setData(pets);
        pageBean.setTotal(count);
        return pageBean;
    }

    @Override
    @Transactional
    public void patchOn(Long[] ids) {
        mapper.patchOn(ids);
    }

    @Override
    @Transactional
    public void patchOff(Long[] ids) {
        mapper.patchOff(ids);
    }

    /**
     * 查询所有宠物品种
     * @return
     */
    @Override
    public List<PetType> findAllType() {
        return petTypeMapper.findAll();
    }

    @Override
    public List<SourceType> findAllSource() {
        return petTypeMapper.findAllSource();
    }
}
